package com.ztesoft.report.controller;


import com.ztesoft.report.base.controller.BaseController;
import com.ztesoft.report.base.pojo.R;
import com.ztesoft.report.entity.SysSql;
import com.ztesoft.report.service.SysSqlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

/**
 * <p>
 * SQL配置 前端控制器
 * </p>
 *
 * @author y3tu
 * @since 2018-06-14
 */
@RestController
@RequestMapping("/sysSql")
public class SysSqlController extends BaseController {
    @Autowired
    SysSqlService sysSqlService;
    private Logger logger = LoggerFactory.getLogger(SysSqlController.class);

    @RequestMapping("/list")
    public Map list(@RequestBody Map<String, Object> map) {
        Map result = null;
        try{
            result = sysSqlService.queryPage(map);
        } catch (Exception e) {
            logger.error("", e);
            result = R.error("查询失败,服务器异常，请联系相关人员！");
        }
        return result;
    }
    @RequestMapping("/getDataSourceList")
    public Map getDataSourceList() {
        Map result = null;
        try{
            result = sysSqlService.getDataSourceList();
        } catch (Exception e) {
            logger.error("", e);
            result = R.error("初始化数据源下拉框失败,服务器异常，请联系相关人员！");
        }
        return result;
    }

    @RequestMapping("/addSysSqlRow")
    public Map addSysSqlRow(@RequestBody SysSql sysSql) {
        sysSql.setCreateBy(getCurrentUser());
        Map result = null;
        try{
            result = sysSqlService.addSysSqlRow(sysSql);
        } catch (Exception e) {
            logger.error("", e);
            result = R.error("新增失败,服务器异常，请联系相关人员！");
        }
        return result;
    }

    @RequestMapping("/updateSysSqlRow")
    public Map updateSysSqlRow(@RequestBody SysSql sysSql) {
        sysSql.setUpdateBy(getCurrentUser());
        Map result = null;
        try{
            result = sysSqlService.updateSysSqlRow(sysSql);
        } catch (Exception e) {
            logger.error("", e);
            result = R.error("编辑失败,服务器异常，请联系相关人员！");
        }
        return result;
    }

    @RequestMapping("/removeSysSqlRow")
    public Map removeSysSqlRow(@RequestBody SysSql sysSql) {
        sysSql.setUpdateBy(getCurrentUser());
        Map result = null;
        try{
            result = sysSqlService.removeSysSqlRow(sysSql);
        } catch (Exception e) {
            logger.error("", e);
            result = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return result;
    }
}
